package com.medical.blood.service;
import com.medical.blood.dto.OperationStatsDTO;
import com.medical.blood.dto.SampleAnalysisDTO;
import com.medical.blood.mapper.OperationStatsMapper;
import com.medical.blood.mapper.SampleAnalysisMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Date;

@Service
public class DataAnalysisService {

    @Autowired
    private OperationStatsMapper operationStatsMapper;
    @Autowired
    private SampleAnalysisMapper sampleAnalysisMapper;

    /**
     * 获取运营数据统计（支持时间范围查询）
     */
    public OperationStatsDTO getOperationStats(Date startTime, Date endTime) {
        OperationStatsDTO stats = new OperationStatsDTO();
        // 设备使用量
        stats.setDeviceUsage(operationStatsMapper.countDeviceUsage(startTime, endTime));
        // 检测项目分布
        stats.setTestItemDistribution(operationStatsMapper.countTestItemDistribution(startTime, endTime));
        // 用户增长趋势
        stats.setUserGrowthTrend(operationStatsMapper.countUserGrowth(startTime, endTime));
        // 日均/月均订单量
        stats.setDailyOrderAvg(operationStatsMapper.countDailyOrderAvg(startTime, endTime));
        stats.setMonthlyOrderAvg(operationStatsMapper.countMonthlyOrderAvg(startTime, endTime));
        // 总订单金额
        stats.setTotalOrderAmount(operationStatsMapper.sumTotalOrderAmount(startTime, endTime));
        return stats;
    }

    /**
     * 获取样本与结果分析数据
     */
    public SampleAnalysisDTO getSampleAnalysis(Date startTime, Date endTime) {
        SampleAnalysisDTO analysis = new SampleAnalysisDTO();
        // 平均送检时长
        analysis.setAvgDeliveryTime(sampleAnalysisMapper.calcAvgDeliveryTime(startTime, endTime));
        // 送检时长分布
        analysis.setDeliveryTimeDistribution(sampleAnalysisMapper.countDeliveryTimeDistribution(startTime, endTime));
        // 异常率（百分比）
        analysis.setAbnormalRate(sampleAnalysisMapper.calcAbnormalRate(startTime, endTime));
        // 异常项目分布
        analysis.setAbnormalItemDistribution(sampleAnalysisMapper.countAbnormalItemDistribution(startTime, endTime));
        return analysis;
    }
}
